<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;

class Goods extends  BaseModel{

    /**
     * 房间门票列表
     */

    public function index(){
        $db_room = db('hotel_room');
        $p=isset($this->requestData['page'])?$this->requestData['page']:'1';
        $size=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $end_date=isset($this->requestData['end_date'])?$this->requestData['end_date']:date('Y-m-d',strtotime("+1 day"));
        $where['r.status']=1;
        $where['r.delete']=1;
        $where['p.roomdate']=strtotime($start_date);
        $where['p.num']=array("gt",0);
        $type=$this->requestData['type'];
        if(!in_array($type,array(0,1,2))) return array('message'=>'类型有误');
        $where['r.type']=$this->requestData['type'];
        $list = $db_room ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.id,r.title,r.image,r.des,p.oprice,p.roomid,p.roomdate,p.price1,p.price2,p.price3,p.price4,p.num")
            ->where($where)
            ->order("r.sort desc,r.id desc")
            ->page($p,$size)
            ->select();
        $level=$this->user['type'];
        foreach($list as &$v){
            switch($level){
                case 1:
                    $v['price']=$v['price1'];
                    break;
                case 2:
                    $v['price']=$v['price2'];
                    break;
                case 3:
                    $v['price']=$v['price3'];
                    break;
                case 4:
                    $v['price']=$v['price4'];
                    break;
            }
            $v['image']=IMG_PATH.$v['image'];
            if($v['des']) {
                $v['des'] = unserialize($v['des']);
            }
        }
        $count = $db_room ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.*,p.oprice,p.roomid,p.roomdate,p.price1,p.price2,p.price3,p.price4")
            ->where($where)
            ->order("r.sort desc,r.id desc")
            ->page($p,$size)
            ->count();
        return array("list"=>$list,'page_num'=>$p,'page_limit'=>$size,'count'=>$count);
    }



    /**
     * 订房页面
     */

    public function sortlist(){
        $db_room = db('hotel_room');
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $where['r.status']=1;
        $where['r.delete']=1;
        $where['p.roomdate']=strtotime($start_date);
        $where['p.num']=array("gt",0);
        $list = $db_room ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.id,r.title,r.image,r.type,p.roomid,p.price1,p.price2,p.price3,p.price4,p.num,r.des")
            ->where($where)
            ->order("r.sort desc,r.id desc")
            ->select();
        $level=$this->user['type'];
        $arr=array();
        $i=0;
        $j=0;
        $k=0;
        foreach($list as &$v){
            switch($level){
                case 1:
                    $v['price']=$v['price1'];
                    break;
                case 2:
                    $v['price']=$v['price2'];
                    break;
                case 3:
                    $v['price']=$v['price3'];
                    break;
                case 4:
                    $v['price']=$v['price4'];
                    break;
            }
            $v['image']=IMG_PATH.$v['image'];
            if($v['des']) {
                $v['des'] = unserialize($v['des']);
            }
            switch($v['type']){
                case 1:
                    if($i<3) {
                        $arr['natatorium'][] = $v;
                    }
                    $i++;
                    break;
                case 2:
                    if($j<3) {
                        $arr['gym'][] = $v;
                    }
                    $j++;
                    break;
                case 0:
                    if($k<3) {
                        $arr['room'][] = $v;
                    }
                    $k++;
                    break;
            }
        }

        return $arr;
    }
    /**
     * 详情
     */
    public function detail(){
        $db_room = db('hotel_room');
        $where['r.id']=intval($this->requestData['id']);
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $where['p.roomdate']=strtotime($start_date);
        $list = $db_room ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.id,r.title,r.images,r.option,r.content,p.oprice,p.roomid,p.roomdate,p.price1,p.price2,p.price3,p.price4,p.num,r.des")
            ->where($where)
            ->order("r.sort desc,r.id desc")
            ->find();
        if(!$list) return array('message'=>'记录不存在');
        $level=$this->user['type'];
        switch($level){
            case 1:
                $list['price']=$list['price1'];
                break;
            case 2:
                $list['price']=$list['price2'];
                break;
            case 3:
                $list['price']=$list['price3'];
                break;
            case 4:
                $list['price']=$list['price4'];
                break;
        }
        if($list['images']) {
            $images = unserialize($list['images']);
            foreach ($images as &$d) {
                $e[] = IMG_PATH . $d;
            }
            $list['images']=$e;
        }
        if($list['option']) {
            $list['option'] = unserialize($list['option']);
        }
        if($list['des']) {
            $room['des'] = unserialize($list['des']);
        }
        return $list;
    }
    /**
     * 房间确定
     */
    public function confirm()
    {
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $end_date=isset($this->requestData['end_date'])?$this->requestData['end_date']:date('Y-m-d',strtotime("+1 day"));
        $db_goods = db('hotel_room');
        $where['p.roomdate'] = strtotime($start_date);
        $where['status']=1;
        $where['delete']=1;
        $where['r.id']=$this->requestData['id'];
        $list = $db_goods ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.id,r.title,r.type,r.type,p.roomid,p.price1,p.price2,p.price3,p.price4,p.num,r.option")
            ->where($where)
            ->find();
        if(!$list) return array('message'=>'记录不存在');
        $level=$this->user['type'];
        switch($level){
            case 1:
                $list['price']=$list['price1'];
                break;
            case 2:
                $list['price']=$list['price2'];
                break;
            case 3:
                $list['price']=$list['price3'];
                break;
            case 4:
                $list['price']=$list['price4'];
                break;
        }
        if($list['option']){
            $list['option']=unserialize($list['option']);
        }
        if($list['type']!=0) return array('message'=>'类型有误');

         $book=(strtotime($end_date)-strtotime($start_date))/(24*60*60);
        for($i=0;$i<$book;$i++) {
            $where['p.roomdate'] = strtotime($start_date) + $i * 24 * 60 * 60;
            $p_num = $db_goods->alias("r")
                ->join("hotel_room_price p", "p.roomid=r.id")
                ->field("p.num")
                ->where($where)
                ->find();
           $allnum[]=$p_num['num'];
        }
        $pos=array_search(min($allnum),$allnum);
        $list['min']= $allnum[$pos];
        $list['start_date']=$start_date;
        $list['end_date']=$end_date;
        $member=db("member")->field("integral")->where("id=".$this->user['id'])->find();
        if(!$list) return array('message'=>'记录不存在');
        return array("goods"=>$list,'member'=>$member);
    }


    /**
     * 确定门票
     */
    public function confirm_ticket()
    {
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $end_date=isset($this->requestData['end_date'])?$this->requestData['end_date']:date('Y-m-d',strtotime("+1 day"));
        $db_goods = db('hotel_room');
        $where['status']=1;
        $where['delete']=1;
        $where['r.id']=$this->requestData['id'];
        $list = $db_goods ->alias("r")
            ->join("hotel_room_price p","p.roomid=r.id")
            ->field("r.id,r.title,r.type,r.type,p.roomid,p.price1,p.price2,p.price3,p.price4,p.num,r.option")
            ->where($where)
            ->find();
        if(!$list) return array('message'=>'记录不存在');
        $level=$this->user['type'];
        switch($level){
            case 1:
                $list['price']=$list['price1'];
                break;
            case 2:
                $list['price']=$list['price2'];
                break;
            case 3:
                $list['price']=$list['price3'];
                break;
            case 4:
                $list['price']=$list['price4'];
                break;
        }
        if($list['option']){
            $list['option']=unserialize($list['option']);
        }
        if(!in_array($list['type'],array(1,2))) return array('message'=>'类型有误');
        $list['start_date']=$start_date;
        $list['end_date']=$end_date;
        $member=db("member")->field("integral")->where("id=".$this->user['id'])->find();

        return array("goods"=>$list,'member'=>$member);
    }

    /**
     * 确定套票
     */
    public function confirm_package()
    {
        $db_goods = db('package');
        $where['status']=1;
        $where['delete']=1;
        $where['id']=$this->requestData['id'];
        $list = $db_goods
            ->field("id,title,price,start_date,end_date,param")
            ->where($where)
            ->find();
        if(!$list) return array('message'=>'记录不存在');
        if($list['param']){
            $list['param']= unserialize($list['param']);
        };
        $list['start_date']=date("Y-m-d",$list['start_date']);
        $list['end_date']=date("Y-m-d",$list['end_date']);
        $member=db("member")->field("integral")->where("id=".$this->user['id'])->find();

        return array("goods"=>$list,'member'=>$member);
    }

    /**
     * 套票详情
     */
    public function packagedetail(){
        $db_package = db('package');
        $where['id']=intval($this->requestData['id']);
        $list = $db_package
            ->where($where)
            ->find();
        if(!$list) return array('message'=>'记录不存在');

        if($list['images']) {
            $images = unserialize($list['images']);
            foreach ($images as &$d) {
                $e[] = IMG_PATH . $d;
            }
            $list['images']=$e;
        }
        if($list['option']) {
            $list['option'] = unserialize($list['option']);
        }
        if($list['param']){
            $list['param']=unserialize($list['param']);
        }
        if($list['start_date']) {
            $list['start_date'] = date("Y-m-d",$list['start_date']);
        }
        if($list['end_date']) {
            $list['end_date'] = date("Y-m-d",$list['end_date']);
        }
        if($list['des']) {
            $room['des'] = unserialize($list['des']);
        }
        return $list;
    }

}